package com.icss.ah.login.service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.WxCorpInfoBean;
import com.icss.ah.login.bean.UserInfoBean;
/**
 * 登录
 * @author csadchen
 * @2016-09-13
 */
@Service
@Transactional
public class LoginService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	/**
	 * 获取用户基本信息
	 * @param userCode
	 * @return
	 */
	public UserInfoBean getUserInfo(String userCode){
		UserInfoBean userInfo = null;
		try{
			//根据登录账号获取账号基本信息
			userInfo = sqlSessionTemplate.selectOne("loginBean.getUserInfo", userCode);
			infolog.info("LoginService|getUserInfo|userInfo:"+userInfo);
			userInfo = userInfo == null ? new UserInfoBean() : userInfo;
		} catch (Exception e) {
			e.printStackTrace();
			userInfo = new UserInfoBean();
			errlog.error("LoginService|getUserInfo", e);
		}
		return userInfo;
	}
	/**
	 * 获取用户登录密码
	 * @param userCode
	 * @return
	 */
	public String getUserPassword(String userCode){
		String password = "";
		try{
			//根据登录账号获取对应密码
			password = sqlSessionTemplate.selectOne("loginBean.getUserPassword", userCode);
			infolog.info("LoginService|getUserPassword|password:"+password);
			password = password == null ? "" : password;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|getUserPassword", e);
		}
		return password;
	}
	/**
	 * 获取公众号信息
	 * @param hm
	 * @return
	 */
	public WxCorpInfoBean getWxCorpInfoBeanByCorpId(HashMap hm){
		WxCorpInfoBean corpInfo = null;
		try{
			//获取公众号信息
			corpInfo = sqlSessionTemplate.selectOne("loginBean.getWxCorpInfoBeanByCorpId", hm);
			infolog.info("LoginService|getWxCorpInfoBeanByCorpId|corpInfo:"+corpInfo);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|getWxCorpInfoBeanByCorpId", e);
		}
		return corpInfo;
	}
	
	/*public String getEmailCode(String userCode){
		String emailCode = "";
		try{
			//根据登录账号获取对应邮箱帐号
			emailCode = sqlSessionTemplate.selectOne("loginBean.getEmailCode", userCode);
			infolog.info("LoginService|getEmailCode|emailCode:"+emailCode);
			emailCode = emailCode == null ? "" : emailCode;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|getUserPassword", e);
		}
		return emailCode;
	}*/
	
	public Map getEmailCode(String userCode){
		Map emailMap = new HashMap();
		try{
			//根据登录账号获取对应邮箱帐号
			emailMap = sqlSessionTemplate.selectOne("loginBean.getEmailCode", userCode);
			infolog.info("LoginService|getEmailCode|emailMap:"+emailMap);
			emailMap = emailMap == null ? new HashMap() : emailMap;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|getEmailCode", e);
		}
		return emailMap;
	}
	/**
	 * 验证账号是否存在
	 * @param userCode
	 * @return
	 */
	public boolean checkUserCodeExist(String userCode){
		boolean result = false;
		try{
			HashMap info = new HashMap();
			info.put("userCode", userCode);
			//根据账号查询是否有对应数据
			Integer count = sqlSessionTemplate.selectOne("loginBean.checkUserCodeExist", info);
			infolog.info("LoginService|checkUserCodeExist|count:"+count);
			if(count>0){
				//该账户已存在
				result = false;
			} else {
				//账号不存在
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|checkUserCodeExist", e);
		}
		return result;
	}
	/**
	 * 保存注册账号
	 * @param userCode
	 * @return
	 */
	@Transactional
	public boolean doRegister(HashMap info){
		boolean result = false;
		try{
			//判断当前账号是否已经存在
			Integer count = sqlSessionTemplate.selectOne("loginBean.checkUserCodeExist", info);
			infolog.info("LoginService|doRegister|count:"+count);
			if(count>0){
				//存在则返回
				result = false;
			} else {
				//不存在则保存注册信息
				info.put("uuid", UUID.randomUUID().toString());
				sqlSessionTemplate.insert("loginBean.doRegister", info);
				infolog.info("LoginService|doRegister|insert:true");
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|doRegister", e);
		}
		return result;
	}
	/**
	 * 验证用户是否拥有店铺（已通过审核的店铺）
	 * @param userCode
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean checkUserShop(String userCode){
		boolean result = false;
		try{
			HashMap info = new HashMap();
			info.put("userCode", userCode);
			//根据账号查询是否有对应数据
			String exist = sqlSessionTemplate.selectOne("loginBean.checkUserShop", info);
			exist = exist == null ?  "false":exist;
			infolog.info("LoginService|checkUserShop|count:"+exist);
			if(exist.equals("false")){
				//未拥有店铺（未申请，或审核中，或审核未通过）
				result = false;
			} else {
				//拥有店铺
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("LoginService|checkUserShop", e);
		}
		return result;
	}
}
